package jzqb.service.impl;

import haha.life.common.BelongToSqlUtils;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.AdminQueryBo;
import jzqb.dao.AdminUserInfoDao;
import jzqb.dao.QBZSDao;
import jzqb.po.AdminUserPo;
import jzqb.po.QBZSPo;
import jzqb.service.AdminQBZSService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 情报知识业务层实现类
 */
@Service
public class AdminQBZSServiceImpl implements AdminQBZSService {

    @Autowired
    private QBZSDao qbzsDao;

    @Autowired
    private AdminUserInfoDao adminUserInfoDao;

    @Override
    public Boolean insertQBZS(QBZSPo po) {
        String year = FileuploadUtils.getYear(po.getPublishDate());
        po.setYear(year);
        return qbzsDao.insertQBZS(po);
    }

    @Override
    public Boolean updateQBZS(QBZSPo po) {
        String year = FileuploadUtils.getYear(po.getPublishDate());
        po.setYear(year);
        return qbzsDao.updateQBZS(po);
    }

    @Override
    public Boolean deleteQBZS(QBZSPo qbzsPo) {
        return qbzsDao.deleteQBZS(qbzsPo);
    }

    @Override
    public List<QBZSPo> queryQBZS(AdminQueryBo bo) {
        //查询情报资源基础信息
        String[] standardType = bo.getStandardType();
        String type ="";
        List<AdminUserPo> allUserList = null;//全部用户
        List<QBZSPo> list = null;
        if(standardType.length == 8){
            type = null;
        }else{
            type = BelongToSqlUtils.getBelongToSql(standardType, "领域");
        }
        bo.setType(type);
        list = qbzsDao.queryQBZS(bo);
        if (list != null && !list.isEmpty()) {
            allUserList = adminUserInfoDao.findAllUser();
            for (QBZSPo po : list) {
                if (allUserList != null && !allUserList.isEmpty()) {
                    for (AdminUserPo userPo : allUserList) {
//                        po.setRecorderName(po.getRecorder());
                        if(po.getRecorder().equals(userPo.getId())){
                            po.setRecorderName(userPo.getName());
                            break;
                        }
                    }

                }
            }
        }
        return list;
    }

    @Override
    public Long count(AdminQueryBo bo) {
        return qbzsDao.count(bo);
    }

    @Override
    public boolean batchImport(String tempDir, JSONObject obj, String dbCode) {
        Boolean result = qbzsDao.batchImport(tempDir, obj, dbCode);
        if(result){
            System.out.println("上传文件：" + obj.get("SYS_FLD_DIGITFILENAME").toString());
        }
        return result;
    }
}
